package com.bra.utils;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;

/**
 * 拍照的工具类
 * 
 * @author houxin
 * 
 */
public class CameraUtilsLister {
	private static final int TAKE_PICTURE = 0;
	private static final int CHOOSE_PICTURE = 1;
	private static final int PHOTOSIZE = 1024 * 1024;
	private static Activity activity;

	@SuppressWarnings("static-access")
	public CameraUtilsLister(Activity activity) {
		this.activity = activity;
	}

	/**
	 * 拍照或者选中相册的lister
	 */
	public OnClickListener camera_lister = new OnClickListener() {
		@Override
		public void onClick(View v) {
			if (v.getTag().equals("拍照")) {
				Intent openCameraIntent = new Intent(
						MediaStore.ACTION_IMAGE_CAPTURE);
				String fileName = "image.jpg";
				Uri imageUri = Uri.fromFile(new File(Environment
						.getExternalStorageDirectory(), fileName));
				// 指定照片保存路径（SD卡），image.jpg为一个临时文件，每次拍照后这个图片都会被替换
				openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
				openCameraIntent.putExtra("return-data", false);
				openCameraIntent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION,
						Configuration.ORIENTATION_PORTRAIT);
				activity.startActivityForResult(openCameraIntent, TAKE_PICTURE);

			} else if (v.getTag().equals("相册")) {
				Intent openAlbumIntent = new Intent(Intent.ACTION_PICK);
				openAlbumIntent
						.setDataAndType(
								MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
								"image/*");
				activity.startActivityForResult(openAlbumIntent, CHOOSE_PICTURE);
			}
		}
	};
	private static Bitmap bitmap;
	private static Bitmap newBitmap;

	/**
	 * 选择照片的图片
	 * 
	 * @return
	 */
	public static Bitmap getRightBitmap() {
		try {
			BitmapFactory.Options opts = new BitmapFactory.Options();
			// 设置inJustDecodeBounds为true
			opts.inJustDecodeBounds = true;
			// 使用decodeFile方法得到图片的宽和高
			BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()
					+ "/image.jpg", opts);
			// 打印出图片的宽和高
			if (((opts.outWidth * opts.outHeight) / PHOTOSIZE) / 2 > 0) {
				int size = ((opts.outWidth * opts.outHeight) / PHOTOSIZE) / 2;
				opts.inSampleSize = size;
			} else {
				opts.inSampleSize = 1;
			}
			opts.inJustDecodeBounds = false;
			bitmap = BitmapFactory.decodeFile(
					Environment.getExternalStorageDirectory() + "/image.jpg",
					opts);
			newBitmap = rotateBitMap(bitmap);
		} catch (OutOfMemoryError e) {
			e.printStackTrace();
		}
		return newBitmap;
	}

	/**
	 * 拍照的照片选择
	 * 
	 * @param originalUri
	 * @return
	 */
	public static Bitmap getRightBitmap(Uri originalUri) {
		try {

			BitmapFactory.Options choose_opts = new BitmapFactory.Options();
			// 设置inJustDecodeBounds为true
			choose_opts.inJustDecodeBounds = true;
			// 使用decodeFile方法得到图片的宽和高
			BitmapFactory.decodeFile(
					ImageUtil.getRealFilePath(activity, originalUri),
					choose_opts);
			// 打印出图片的宽和高
			if (((choose_opts.outWidth * choose_opts.outHeight) / PHOTOSIZE) / 2 > 0) {
				int size = ((choose_opts.outWidth * choose_opts.outHeight) / PHOTOSIZE) / 2;
				choose_opts.inSampleSize = size;
			} else {
				choose_opts.inSampleSize = 1;
			}
			choose_opts.inJustDecodeBounds = false;
			bitmap = BitmapFactory.decodeFile(
					ImageUtil.getRealFilePath(activity, originalUri),
					choose_opts);

		} catch (OutOfMemoryError e) {
			e.printStackTrace();
		}
		return bitmap;
	}

	static Bitmap rotateBitMap(Bitmap bp) {
		Matrix matrix = new Matrix();
		matrix.reset();
		matrix.postRotate(90);
		Bitmap nowBp = Bitmap.createBitmap(bp, 0, 0, bp.getWidth(),
				bp.getHeight(), matrix, true);
		if (bp.isRecycled()) {
			bp.recycle();
		}
		return nowBp;
	}
}